/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jgaf.exporter;


import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import jgaf.automaton.fa.AutomatonPainter;
import jgaf.automaton.fa.Painter;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.svggen.SVGGraphics2DIOException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;

/**
 *
 * @author hanis
 */
public class SVGExporter {

    public static final String SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";

    public static void exportAutomatonGraphics(Painter painter, File file) throws UnsupportedEncodingException, SVGGraphics2DIOException, FileNotFoundException {
     //   painter.getEditor().moveGraphicsToTheOrigin();
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
      
        Document document = domImpl.createDocument(SVG_NAMESPACE_URI, "svg", null);
       
        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
        ctx.setComment("Generated by FooApplication with Batik SVG Generator");
        SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);

        //SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        
       // painter.updateGraphicsForSVG(svgGenerator);
        painter.updateGraphicsForSVG(svgGenerator);
        
        boolean useCSS = true; 
        Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        svgGenerator.stream(out, useCSS);               
    }


    public static void exportComponentGraphics(Component component, File file) throws UnsupportedEncodingException, SVGGraphics2DIOException, FileNotFoundException {
     //   painter.getEditor().moveGraphicsToTheOrigin();
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();

        Document document = domImpl.createDocument(SVG_NAMESPACE_URI, "svg", null);

        SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document);
        ctx.setComment("Generated by FooApplication with Batik SVG Generator");
        SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false);

        //SVGGraphics2D svgGenerator = new SVGGraphics2D(document);





        component.paint(svgGenerator);
       // svgGenerator.clipRect(0, 0, (int) component.getPreferredSize().getWidth(), (int) component.getPreferredSize().getHeight());

        boolean useCSS = true;
        Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
        svgGenerator.stream(out, useCSS);
    }

}
